package com.dzpk.gameserver.data.hot;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import somnus.net.DefaultHandler;

public class GdServerHandler extends DefaultHandler {

    public GdServerHandler(String name) {
        super(name);
    }

    private final static Logger logger = LoggerFactory.getLogger(GdServerHandler.class);

    public void init() {
        logger.info("Hot GdServer init.......");
    }

    @Override
    public void handleMessage(IoSession uSession, Object object) throws Exception {

        String cmd = (String) object;
        System.out.println("cmd..................: " + cmd);
        if (cmd == null || cmd.trim().length() == 0) {
            uSession.write("error command is null");
            return;
        } else if (cmd.equalsIgnoreCase("quit") || cmd.equalsIgnoreCase("exit")) {
            uSession.close(true);
            return;
        } else {
            String[] str = cmd.split(":");
            if (str.length < 2) {
                uSession.write("error command and args...");
                return;
            }
            String[] args = getArgs(str[0], str[1].trim());
            if (args == null) {
                uSession.write(String.format("error args...(%s)", str[1]));
                return;
            }

            if ("gd".equals(str[0])) {
//                storeGdFile(uSession, args[0]);
            }
        }
    }

    private String[] getArgs(String cmd, String args) {
        String[] res = null;
        if ("gd".equals(cmd)) {
            res = new String[1];
            if (args.trim().length() > 0) {
                res[0] = args.trim();
            } else {
                return null;
            }
        }
        return res;
    }


    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        session.close(true);
    }

    public void destroy() {
        logger.info("Hot GdServer closed.......");
    }
}
